iT邦幫忙

2024 iThome 鐵人賽

DAY 29
0
自我挑戰組

Web Franework -SASS/SCSS系列 第 29

Day 29 - 我決定把他做成一個記憶力遊戲

  • 分享至 

  • xImage
  •  

HTML

<div class="game-container">
  <div class="grid">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
    <div class="item">7</div>
    <div class="item">8</div>
    <div class="item">9</div>
  </div>
  <div class="score-board">
    <h2>成功次數: <span id="success-count">0</span></h2>
  </div>
</div>

Sass

body
  font-family: Arial, sans-serif
  display: flex
  justify-content: center
  align-items: center
  height: 100vh
  background-color: #f5f6f7

.game-container
  display: flex
  flex-direction: row
  justify-content: center
  align-items: center
  margin: 20px

.grid
  display: grid
  grid-template-columns: repeat(3, 100px)
  grid-template-rows: repeat(3, 100px)
  gap: 10px

.item
  background-color: #85d7ff 
  display: flex
  justify-content: center
  align-items: center
  font-size: 24px
  border-radius: 10px
  cursor: pointer
  color: #000  

.score-board
  margin-left: 20px
  font-size: 20px

Javascript

const items = document.querySelectorAll('.item');
const successCountElement = document.getElementById('success-count');
let successCount = 0;

let order = [...Array(9).keys()].map(i => i + 1);
let playerOrder = [];
let currentStep = 0;
let isGameActive = false;

// 開始遊戲
function startGame() {
    items.forEach((item, index) => {
        item.textContent = order[index];
        item.style.backgroundColor = '#85d7ff';
        item.style.pointerEvents = 'none';
    });

    // 3秒後隱藏數字,改變按鈕顏色
    setTimeout(() => {
        items.forEach(item => {
            item.style.backgroundColor = '#003366';
        });

        setTimeout(() => {
            items.forEach((item) => {
                item.textContent = '';
                item.style.pointerEvents = 'auto';
            });
            isGameActive = true;
        }, 0);
    }, 3000);
}

// 處理按鈕點擊事件
function handleClick(item, index) {
    if (!isGameActive) return;
    
    if (parseInt(item.textContent) === order[currentStep]) {
        item.textContent = '';
        playerOrder.push(order[currentStep]);
        currentStep++; 

        if (currentStep === order.length) {
            successCount++;
            successCountElement.textContent = successCount;
            resetGame();
        }
    } else {
        alert("錯誤!請重新嘗試。");
        resetPlayerProgress();
    }
}

// 重置遊戲
function resetGame() {
    currentStep = 0;
    playerOrder = [];
    order = shuffleArray([...Array(9).keys()].map(i => i + 1)); // 隨機打亂按鈕的數字
    startGame();
}

// 重置玩家的進度
function resetPlayerProgress() {
    currentStep = 0;
    playerOrder = [];
}

// 隨機打亂數組
function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
}

// 初始化遊戲
startGame();
items.forEach((item, index) => {
    item.addEventListener('click', () => handleClick(item, index));
});

明天最後一天。
加油!


上一篇
Day 28 - 稍微新增了一些功能
下一篇
Day30 - 心得
系列文
Web Franework -SASS/SCSS30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言